const db = require("../db/index")
exports.getLike=(req,res)=>{
    let id=req.auth.id
    if(!id)return res.R("登录已过期!")
    let sql="select bid,id from islike where uid = ?"
    db.query(sql,[id],(err,results)=>{
        if(err)return res.R(err)
        res.send({
            status:200,
            message:"查询成功！",
            data:results
        })
    })
}
exports.addLike=(req,res)=>{
    let id=req.auth.id
    let bid=req.params.bid
    let sql="select id from islike where uid = ? and bid = ?"
    db.query(sql,[id,bid],(err,results)=>{
        if (err) return res.R(err)
        if (results.length!==0){
            let likeid =results[0].id
            let removesql="delete from islike where id = ?"
            db.query(removesql,[likeid],(err,results2)=>{
                if(err)return res.R(err)
                if(results2.affectedRows!==1)return res.R("取消点赞失败！")
                return res.send({
                    status:200,
                    message:"点赞已取消！"
                })
            })
        }
        else{
            let insertsql="insert into islike (uid,bid) values(?,?)"
            db.query(insertsql,[id,bid],(err,results3)=>{
                if(err)return res.R(err)
                if(results3.affectedRows!==1)return res.R("点赞失败！")
                return res.send({
                    status:200,
                    message:"已点赞！"
                })
            })
        }
    })

}